package com.example.uploadattachment.model;

import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
import java.util.List;

/**
 * 全局统一返回类
 *
 * @author 何逸诚
 * @date 2023/12/6 15:39
 */
@Data
@ToString
public class ResultResponse<T> implements Serializable {
    private final Integer code;

    private String msg;

    private T data;

    private final Long dataCount;

    private String traceId;

    /**
     * 构造函数
     */
    public ResultResponse(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        this.data = null;
        this.dataCount = null;
    }

    public ResultResponse(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.dataCount = null;
    }

    public ResultResponse(Integer code, String msg, T data, Long dataCount) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.dataCount = dataCount;
    }

    /**
     * 给msg赋值，连缀风格
     */
    public ResultResponse<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    /**
     * 给data赋值，连缀风格
     */
    public ResultResponse<T> setData(T data) {
        this.data = data;
        return this;
    }

    /**
     * 返回成功
     */
    public static <T> ResultResponse<T> ok() {
        return new ResultResponse<>(200, "ok");
    }

    public static <T> ResultResponse<T> ok(String msg) {
        return new ResultResponse<>(200, msg);
    }

    public static <T> ResultResponse<T> ok(T data) {
        return new ResultResponse<>(200, "ok", data);
    }

    public static <T> ResultResponse<T> ok(String msg, T data) {
        return new ResultResponse<>(200, msg, data);
    }

    public static ResultResponse<Object> okArray(Object... data) {
        return new ResultResponse<>(200, "ok", data);
    }

    /**
     * 返回失败
     */
    public static <T> ResultResponse<T> fail() {
        return new ResultResponse<>(0, "error");
    }

    public static <T> ResultResponse<T> fail(String msg) {
        return new ResultResponse<>(0, msg);
    }

    public static <T> ResultResponse<T> fail(T data) {
        return new ResultResponse<>(0, "error", data);
    }

    public static <T> ResultResponse<T> fail(String msg, T data) {
        return new ResultResponse<>(0, msg, data);
    }

    public static ResultResponse<List<String>> fail(String msg, List<String> data) {
        return new ResultResponse<>(0, msg, data);
    }

    /**
     * 返回一个自定义状态码
     */
    public static <T> ResultResponse<T> build(Integer code, String msg) {
        return new ResultResponse<>(code, msg);
    }

    /**
     * 返回分页和数据
     */
    public static ResultResponse<Object> getPageData(Long dataCount, Object data) {
        return new ResultResponse<>(200, "ok", data, dataCount);
    }

    /**
     * 返回，根据布尔值来确定最终结果(true = ok, false = fail)
     */
    public static <T> ResultResponse<T> getByBoolean(boolean b) {
        return b ? ok("ok") : fail("fail");
    }
}
